import {Component, OnInit} from '@angular/core';
import {DeviceService} from '../../../svc/device.svc';
import {MessageService} from 'primeng/api';
import {ListboxModule} from 'primeng/listbox';

@Component({
    selector: 'app-network',
    templateUrl: './network.component.html',
    styleUrls: ['./network.component.css']
})
export class NetworkComponent implements OnInit {
    networkTableStatus: boolean;
    securityTableStatus: boolean;
    display: boolean;
    ssidDisplay: boolean;
    selectedSSID;
    apData;
    selectedApData;
    dialogApPasswdSwitch = false;
    dialogApPasswdHeader;
    dialogApPassword;
    currentAP;

    WLANAPInformation = {
        SSID: '',
        Password: '',
    };
    Security = [
        {code: 'Open'},
        {code: 'WEP'},
        {code: ' WPA/WPA2 PSK'},
        {code: ' WPA2 PSK'},
    ];
    selectedCountyCode;

    WLANClientInformation = {
        nodeName: '',
        SSID: '',
        password: '',
        ipAddress: '',
        netmask: '',
        gateway: '',
        primaryDNS: '',
        secondaryDNS: '',
    };
    WLANSecurity = [
        {type: 'Open'},
        {type: 'WEP'},
        {type: 'WPA/WPA2 PSK'},
        {type: 'WPA2 PSK'},
    ];
    selectedWLANSecurity;
    addressType = [
        {type: 'DHCP'},
        {type: 'Static'},
    ];
    selectedAddressType;

    wifiData = [
        {ssid: 'BCGMCU'},
        {ssid: 'ESP32'},
        {ssid: 'RHCT'},
        {ssid: 'RHCT5G'},
        {ssid: 'RHCT6G'},
        {ssid: 'RHCT7G'},
        {ssid: 'RHCT8G'},
    ];

    cities = [
        {name: 'New York', code: 'NY'},
        {name: 'Rome', code: 'RM'},
        {name: 'London', code: 'LDN'},
        {name: 'Istanbul', code: 'IST'},
        {name: 'Paris', code: 'PRS'},
        {name: '2', code: 'PRS'},
        {name: '3', code: 'PRS'},
        {name: '4', code: 'PRS'},
        {name: '5', code: 'PRS'}
    ];

    constructor(public ds: DeviceService, private ms: MessageService) {
    }

    ngOnInit(): void {
        this.getApJson();
        this.getStatusJson();
    }

    networkTableSwitch(): void {
        this.networkTableStatus = this.selectedAddressType.type !== 'DHCP';
    }

    securityTableSwitch(): void {
        this.securityTableStatus = this.selectedWLANSecurity.type !== 'Open';
    }

    getApJson(): void {
        this.ds.getApJson().subscribe(data => {
            // this.apData = this.wifiData;
            this.apData = data;
            console.log(data);
        });
    }

    getStatusJson(): void {
        console.log('Get Status');
        this.ds.getStatusJson().subscribe(data => {
            this.currentAP = data.ssid;
            console.log(data);
        });
    }

    apConnect(): void {
        const ssid = this.dialogApPasswdHeader;
        const password = this.dialogApPassword;
        this.ds.setConnect(ssid, password).subscribe(data => {
            this.currentAP = this.selectedApData[0].ssid;
            this.closePasswdDialog();
            this.ms.add({severity: 'success', summary: 'Success', life: 1000});
        });
    }

    disConnect(): void {
        this.ds.disConnect().subscribe(data => {
            this.currentAP = '';
            this.ms.add({severity: 'success', summary: 'Success', life: 1000});
        });
    }

    openPasswdDialog(): void {
        this.dialogApPasswdSwitch = true;
        this.dialogApPasswdHeader = this.selectedApData[0].ssid;
    }

    closePasswdDialog(): void {
        this.dialogApPasswdSwitch = false;
        this.dialogApPassword = this.dialogApPasswdHeader = '';
    }

    selectSSID() {
        this.ssidDisplay = true;
    }

    selectCancel() {
        this.ssidDisplay = false;
    }

    saveSelectSSID() {

        this.WLANClientInformation.SSID = this.selectedSSID.name;
        this.ssidDisplay = false;
        console.log(this.WLANClientInformation)
    }

    applyChange() {
        this.display = true;
    }
    apply(){

    }

    cancel() {
        this.display = false;
    }
}
